import 'package:flutter/material.dart';

/**
 * 局部刷新组件
 *
 * https://www.jianshu.com/p/05271e7928fa
 */
abstract class RefreshableStatefulWidget extends StatefulWidget {
  RefreshableStatefulWidget({Key key})
      : super(key: key is GlobalKey ? key : GlobalKey());

  void reload() {
    if (key is! GlobalKey) {
      return;
    }
    final aKey = key as GlobalKey;
    aKey.currentState.setState(() {});
  }
}

class RefreshableView extends RefreshableStatefulWidget {
  final Widget Function(BuildContext cntext) builder;

  RefreshableView({Key key, @required this.builder})
      : assert(builder != null),
        super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _RefreshableViewState(builder);
  }
}

class _RefreshableViewState extends State<RefreshableView> {
  final Widget Function(BuildContext cntext) builder;

  _RefreshableViewState(this.builder);

  @override
  Widget build(BuildContext context) {
    return builder(context);
  }
}
